<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/31
 * Time: 11:47
 */

namespace App\Http\Controllers\Api\V1;


use DB;
use App\Repositories\RecruitRepository;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Facades\JWTAuth;
use App\Libs\Umeng;

class RecruitController extends BaseController
{
    protected $recruitRepository;
    public function __construct(RecruitRepository $recruitRepository)
    {
        $this->recruitRepository = $recruitRepository;
    }
    /**
     * @api {get} /recruit/get_set 获取驾校推广设置(recruit get_set)
     * @apiDescription 获取驾校推广设置
     * @apiGroup Recruit
     * @apiSampleRequest /api/recruit/get_set
     * @apiPermission none
     * @apiParam {int} [school_id]  school_id驾校ID
     * @apiParam {int} [type]  type 设置类型
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *  {
            "message": "success",
            "code": 200,
            "data": {
                "prepay_money": "500",
                "privilege_money": "200",
                "superior_money": "60",
                "superior_superior_money": "30",
                "remark": "这是个测试数据"
            }
        }
     */
    public function getSet(Request $request)
    {
        $school_id = $request->get('school_id');
        $type = $request->get('type');
        if(empty($school_id) || empty($type))
        {
            return $this->errorResponse('参数错误', 401);
        }
        $data = $this->recruitRepository->getSet($school_id,$type);
        return $this->successResponse('success',['data'=>$data]);
    }
    /**
     * @api {post} /recruit/add_extension 新增推广人员(recruit add_extension)
     * @apiDescription 新增推广人员
     * @apiGroup Recruit
     * @apiSampleRequest /api/recruit/add_extension
     * @apiPermission none
     * @apiParam {int} [school_id]  school_id驾校ID
     * @apiParam {String} [nickname]  nickname 微信昵称
     * @apiParam {String} [open_id]  open_id 微信open_id
     * @apiParam {String} [img]  img 微信头像
     * @apiParam {String} [qr_code]  qr_code 推广员二维码地址
     * @apiParam {int} [issign]  issign 是否报名
     * @apiParam {int} [p_id]  p_id 上级ID
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
            "message": "操作成功",
            "code": 200,
            "id": 3
        }
     */
    public function addExtension(Request $request)
    {
        $data['school_id'] = $request->get('school_id');
        $data['nickname'] = $request->get('nickname');
        $data['open_id'] = $request->get('open_id');
        $data['p_id'] = $request->get('p_id',0);
        $data['qr_code'] = $request->get('qr_code');
        if($data['p_id'] != 0)
        {
            $data['up_nickname'] = DB::table('extensions')->where('id',$data['p_id'])->value('nickname');
        }
        $data['img'] = $request->get('img','');
        $data['issign'] = $request->get('issign',0);
        if(empty($data['school_id']) || empty($data['nickname']) || empty($data['open_id']) )return $this->errorResponse('参数错误', 401);
        $info = DB::table('extensions')->where('open_id',$data['open_id'])->first();

        $school_id = $data['school_id'];
        $type = 6;
        $set = $this->recruitRepository->getSet($school_id,$type);
        if(!empty($set))
        {
            if($set->ischeck == 2)$data['status']=1;
        }
        if(!empty($info))
        {
            return $this->successResponse('操作成功',['id'=>$info->id]);
        }
        $extension_id = DB::table('extensions')->insertGetId($data);
        if($extension_id)
        {
            return $this->successResponse('操作成功',['id'=>$extension_id]);
        }else{
            return $this->errorResponse('操作失败', 401);
        }
    }
    /**
     * @api {post} /recruit/update_extension 更新推广人员(recruit update_extension)
     * @apiDescription 更新推广人员
     * @apiGroup Recruit
     * @apiSampleRequest /api/recruit/update_extension
     * @apiPermission none
     * @apiParam {int} [id]  id 推广员ID
     * @apiParam {String} [data]  data 更新数据 json格式 {"one_recoomendation":1,"two_recommendation":2,"share_friend":3}
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *        "message": "success",
    "code": 200,
    "data": {
    }
     *   }
     */
    public function updateExtension(Request $request)
    {
        $id = $request->get('id');
        $data = $request->get('data');
        if(empty($id))return $this->errorResponse('参数错误', 401);
        $count = DB::table('extensions')->where('id',$id)->count();
        if($count < 1) return $this->errorResponse('用户不存在', 401);
        if(DB::table('extensions')->where('id',$id)->update($data))
        {
            return $this->successResponse('操作成功');
        }else{
            return $this->errorResponse('操作失败', 401);
        }
    }
    /**
     * @api {post} /recruit/get_extension 获取推广人员信息(recruit get_extension)
     * @apiDescription 获取推广人员信息
     * @apiGroup Recruit
     * @apiSampleRequest /api/recruit/get_extension
     * @apiPermission none
     * @apiParam {int} [id]  id 推广员ID
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     * "message": "success",
     * "code": 200,
     * "data": {
     * "id": 1,
     * "nickname": "5566",
     * "open_id": "1234564964981",
     * "one_recoomendation": 0,
     * "two_recommendation": 0,
     * "share_friend": 0,
     * "share_friends": 0,
     * "sign_count": 0,
     * "commission": 0,
     * "issign": 1,
     * "up_nickname": "",
     * "created_at": "2018-01-29 15:30:28",
     * "updated_at": "0000-00-00 00:00:00",
     * "status": 1,
     * "school_id": 36,
     * "p_id": 0,
     * "img": "",
     * "frozen_commission": 0
     * }
     * }
     */
    public function getExtension(Request $request)
    {
        $id = $request->get('id');
        if(empty($id))return $this->errorResponse('参数错误', 401);
        $info = DB::table('extensions')->where('id',$id)->first();
        if(empty($info)) return $this->errorResponse('用户不存在', 401);
        return $this->successResponse('success',['data'=>$info]);
    }
    /**
     * @api {post} /recruit/sign_up 学员报名(recruit sign_up)
     * @apiDescription 学员报名
     * @apiGroup Recruit
     * @apiSampleRequest /api/recruit/sign_up
     * @apiPermission none
     * @apiParam {int} [school_id]  school_id驾校ID
     * @apiParam {String} [userphone]  userphone 用户电话
     * @apiParam {String} [serial_number]  serial_number 预付流水号
     * @apiParam {String} [open_id]  open_id 微信open_id
     * @apiParam {String} [nickname]  nickname 姓名
     * @apiParam {String} [img]  img 微信头像
     * @apiParam {int} [extension_id]  extension_id 推广员id
     * @apiParam {int} [my_extension_id]  my_extension_id 自身推广员id
     * @apiParam {int} [service_id]  service_id 版型id
     * @apiParam {float} [prepay_money]  prepay_money 网上预付金额
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *        "message": "success",
    "code": 200,
    "data": {
    }
     *   }
     */
    public function signUp(Request $request)
    {
        $data['school_id'] = $request->get('school_id');
        $data['nickname'] = $request->get('nickname');
        $data['img'] = $request->get('img','');
        $data['userphone'] = $request->get('userphone','');
        $data['serial_number'] = $request->get('serial_number');
        $data['open_id'] = $request->get('open_id');
        $data['prepay_money'] = $request->get('prepay_money');
        $data['extension_id'] = $request->get('extension_id',0);
        $my_extension_id = $request->get('my_extension_id',0);
        $service_id = $request->get('service_id');
        $service = DB::table('services')->where('id',$service_id)->first();
        if(empty($service)) return $this->errorResponse('参数错误,班形id错误', 401);
        $extension = DB::table('extensions')->where('id',$data['extension_id'])->first();
        $data['img'] =  DB::table('extensions')->where('id',$my_extension_id)->value('img');
        if($this->recruitRepository->signUp($data,$service,$extension,$my_extension_id))
        {
            return $this->successResponse('操作成功');
        }else{
            return $this->errorResponse('操作失败', 401);
        }
    }
    /**
     * @api {get} /recruit/get_record 获取佣金列表(recruit get_record)
     * @apiDescription 获取佣金列表
     * @apiGroup Recruit
     * @apiSampleRequest /api/recruit/get_record
     * @apiPermission none
     * @apiParam {int} [extension_id]  extension_id 推广人员ID
     * @apiParam {int} [status]  status 状态 0:在路上 1:成功
     * @apiParam {int} [page]  page 页数
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
        "message": "success",
        "code": 200,
        "data": [
                {
                "status": 1, //状态 0:在路上 1:成功
                "money": 50,
                "created_at": "2018-01-30 12:16:44",
                "img": "", //微信头像
                "nickname": "" //名称
                },
                {
                "status": 1,
                "money": 50,
                "created_at": "2018-01-30 12:16:56",
                "img": "",
                "nickname": ""
                }
            ]
        }
     */
    public function getRecord(Request $request)
    {
        $extension_id = $request->get('extension_id');
        $status = $request->get('status',0);
        $lists = DB::table('extension_records')->where('extension_id',$extension_id)->where('status',$status)->select('status','money','created_at','img','nickname')->paginate(15)->toArray();
        if($lists['total']>0)
        {
            return $this->successResponse('success',['data'=>$lists['data']]);
        }else
        {
            return $this->successResponse('success',['data'=>null]);
        }
    }
    /**
     * @api {post} /recruit/withdraw 佣金提现(recruit withdraw)
     * @apiDescription 学员报名
     * @apiGroup Recruit
     * @apiSampleRequest /api/recruit/withdraw
     * @apiPermission none
     * @apiParam {String} [name]  name 姓名
     * @apiParam {String} [tel]  tel 手机号
     * @apiParam {int} [extension_id]  extension_id 推广员id
     * @apiParam {float} [money]  money 提现金额
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *        "message": "success",
    "code": 200,
    "data": {
    }
     *   }
     */
    public function withdraw(Request $request)
    {
        $extension_id = $request->get('extension_id');
        $money = $request->get('money');
        $name = $request->get('name');
        $tel = $request->get('tel');
        if($money<0 || empty($extension_id) || empty($name) || empty($tel)) return $this->errorResponse('参数错误', 401);
        $returnStatus = $this->recruitRepository->withdraw($extension_id,$money,$name,$tel);
        if($returnStatus['status'] == 1)
        {
            return $this->successResponse('操作成功');
        }else{
            return $this->errorResponse($returnStatus['message'], 401);
        }
    }
    /**
     * @api {get} /recruit/get_school 获取驾校信息(recruit get_school)
     * @apiDescription 获取驾校信息
     * @apiGroup Recruit
     * @apiSampleRequest /api/recruit/get_school
     * @apiPermission none
     * @apiParam {int} [school_id]  school_id  驾校id
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
        {
            "message": "success",
            "code": 200,
            "data": {
                "content": "火热和华为", //简介
                "name": "神通测试驾校", //名称
                "pic": "null", //风采图片
                "logo": "" //logo
            }
        }
     */
    public function getSchool(Request $request)
    {
        $id = $request->get('school_id');
        if(empty($id)) return $this->errorResponse('参数错误', 401);
        $school = DB::table('schools')->where('id',$id)->first();
        $content = DB::table('styles')->where('item_id',$id)->where('type',1)->first();
        if(empty($school)) return $this->errorResponse('参数错误', 401);
        $data['content'] = is_object($content)?$content->content:'';
        $data['name'] = is_object($school)?$school->name:'';
        $data['pic'] = is_object($content)?$content->pic:'';
        $data['logo'] = is_object($content)?$content->logo:'';
        return $this->successResponse('success',['data'=>$data]);
    }
    /**
     * @api {get} /recruit/get_service 获取驾校版型列表(recruit get_service)
     * @apiDescription 获取驾校版型列表
     * @apiGroup Recruit
     * @apiSampleRequest /api/recruit/get_service
     * @apiPermission none
     * @apiParam {int} [school_id]  school_id 驾校id
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
    {
        "message": "success",
        "code": 200,
        "data": [
            {
            "service": "", //版型介绍
            "classcurr": "viip", //版型名称
            "price": "111.00", //报名总价
            "prepay_money": "500", //预付费额
            "privilege_money": "200" //优惠总额
            },
            {
            "service": "",
            "classcurr": "测2",
            "price": "3830.00",
            "prepay_money": "500",
            "privilege_money": "200"
            },
            {
            "service": "321",
            "classcurr": "134321222",
            "price": "83.00",
            "prepay_money": "500",
            "privilege_money": "200"
            },
            {
            "service": "321",
            "classcurr": "321312",
            "price": "32.00",
            "prepay_money": "500",
            "privilege_money": "200"
            },
            {
            "service": "",
            "classcurr": "1211",
            "price": "111.00",
            "prepay_money": "500",
            "privilege_money": "200"
            }
        ]
    }
     */
    public function getService(Request $request)
    {
        $id = $request->get('school_id');
        if(empty($id)) return $this->errorResponse('参数错误', 401);
        $list = DB::table('services')->where('school_id',$id)->select('id','service','classcurr','price')->get()->toArray();
        $set = $this->recruitRepository->getSet($id,2);
        foreach ($list as $v)
        {
            $v->prepay_money = $set->prepay_money;
            $v->privilege_money = $set->privilege_money;
        }
        return $this->successResponse('success',['data'=>$list]);
    }
}